home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / TestTools / Sources / TestExceptions.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  3.1 KB  |  139 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestExceptions.cp
  3.  
  4.     Contains:    Implementation of class TTestExceptions
  5.  
  6.     Copyright:    © 1992-1995 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __TESTEXCEPTIONS__
  11. #include "TestExceptions.h"
  12. #endif
  13.  
  14. /*******************************************************************************
  15. ** External Function
  16. ********************************************************************************/
  17.  
  18. extern void ThrowException(long);
  19.  
  20. /*******************************************************************************
  21. ** TTestExceptions routine
  22. ********************************************************************************/
  23.  
  24. TTestExceptions::TTestExceptions()
  25. {}
  26.  
  27. TTestExceptions::~TTestExceptions()
  28. {}
  29.  
  30. void TTestExceptions::InitTest(BooleanParm, BooleanParm, int, char**)
  31. {}
  32.  
  33. void TTestExceptions::EndTest(BooleanParm, BooleanParm)
  34. {}
  35.  
  36. void TTestExceptions::RunTestIteration(BooleanParm verbose, BooleanParm debug)
  37. {
  38.     DebugTest(debug, "About to Test Exceptions");
  39.     if (verbose)
  40.         Printf("# INFO: Testing CATCH\n");
  41.         
  42.     TRY
  43.         ThrowException(-10);
  44.     CATCH(-10)
  45.         if (verbose)
  46.             Printf("# INFO: Caught the Exception!\n");
  47.     CATCH_ALL
  48.         Printf("# ERROR: Exception caught in CATCH_ALL - #%d\n", ErrorCode());
  49.     ENDTRY
  50.  
  51.     if (verbose)
  52.         Printf("# INFO: Testing multiple CATCHs\n");
  53.         
  54.     TRY
  55.         ThrowException(-20);
  56.     CATCH(-10)
  57.         Printf("# ERROR: Exception caught in CATCH(-10) - #%d\n", ErrorCode());
  58.     CATCH(-20)
  59.         if (verbose)
  60.             Printf("# INFO: Caught the Exception!\n");
  61.     CATCH(-30)
  62.         Printf("# ERROR: Exception caught in CATCH(-30) - #%d\n", ErrorCode());
  63.     CATCH_ALL
  64.         Printf("# ERROR: Exception caught in CATCH_ALL - #%d\n", ErrorCode());
  65.     ENDTRY
  66.  
  67.     if (verbose)
  68.         Printf("# INFO: Testing CATCH_ALL\n");
  69.         
  70.     TRY
  71.         ThrowException(-21);
  72.     CATCH(-10)
  73.         Printf("# ERROR: Exception caught in CATCH(-10) - #%d\n", ErrorCode());
  74.     CATCH(-20)
  75.         Printf("# ERROR: Exception caught in CATCH(-20) - #%d\n", ErrorCode());
  76.     CATCH(-30)
  77.         Printf("# ERROR: Exception caught in CATCH(-30) - #%d\n", ErrorCode());
  78.     CATCH_ALL
  79.         if (ErrorCode() == -21)
  80.         {
  81.             if (verbose)
  82.                 Printf("# INFO: Exception caugh in CATCH_ALL\n");
  83.         }
  84.         else
  85.             Printf("# ERROR: Exception caught in CATCH_ALL - #%d\n", ErrorCode());
  86.     ENDTRY
  87.  
  88.  
  89.     if (verbose)
  90.         Printf("# INFO: Testing FINALLY WITH EXCEPTION\n");
  91.         
  92.     Volatile Boolean flag1 = false;
  93.     Volatile Boolean flag2 = false;
  94.     VOLATILE(flag1);
  95.     VOLATILE(flag2);
  96.     
  97.     TRY
  98.         TRY
  99.             RAISE(-10);
  100.         FINALLY
  101.             flag1 = true;
  102.             if (verbose)
  103.                 Printf("# INFO: Reached FINALLY code\n");
  104.         ENDTRY
  105.     CATCH_ALL
  106.         flag2 = true;
  107.         if (verbose)
  108.             Printf("# INFO: Reached outer CATCH code\n");
  109.     ENDTRY
  110.  
  111.     if (!flag1)
  112.         Printf("# ERROR: Never reached FINALLY code\n");
  113.     if (!flag2)
  114.         Printf("# ERROR: FINALLY section did not rethrow exception\n");
  115.     
  116.     
  117.     if (verbose)
  118.         Printf("# INFO: Testing FINALLY WITHOUT EXCEPTION\n");
  119.  
  120.     Volatile Boolean flag = false;
  121.     VOLATILE(flag);
  122.         
  123.     TRY
  124.         TRY
  125.             // nothing to do here
  126.         FINALLY
  127.             flag = true;
  128.             if (verbose)
  129.                 Printf("# INFO: Reached FINALLY code\n");
  130.         ENDTRY
  131.     CATCH_ALL
  132.         if (verbose)
  133.             Printf("# ERROR: FINALLY section rethrew exception\n");
  134.     ENDTRY
  135.     
  136.     if (!flag)
  137.         Printf("# ERROR: Never reached FINALLY code\n");
  138. }
  139.